<?php
namespace backend\controllers;

use backend\models\User;
use Yii;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use backend\models\LoginForm;

/**
 * Site controller
 */
class SiteController extends BaseController
{

    const CAPTCHA_VALUE = 'EASYDO';

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error', 'captcha'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'password', 'index', 'theme'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
                'layout' => user()->isGuest?'main-error':'main',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_DEBUG?self::CAPTCHA_VALUE:null,
                'minLength' => 4,
                'foreColor' => '200000',
                'maxLength' => 5,
                'padding' => 4,
                'offset' => 10,
                'fontFile' => '@webroot/css/font/books.ttf'
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * actionLogin
     * @return string|\yii\web\Response
     */
    public function actionLogin()
    {
        $this->layout = 'main-login';

        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();

        $model->captcha = YII_DEBUG?self::CAPTCHA_VALUE:null;

        if ($model->load(request()->post())) {
            if($model->login()){
                return $this->goBack();
            }else{
                $this->sessionError(getModelErrorMsg($model,'登录失败.'));
            }
        }

        return $this->render('login', [
            'model' => $model,
        ]);
    }

    /**
     * actionLogout
     * @return \yii\web\Response
     */
    public function actionLogout()
    {
        user()->logout();

        return $this->goHome();
    }

    /**
     * actionPassword
     * @return \yii\web\Response
     * @throws \yii\base\Exception
     */
    public function actionPassword()
    {
        $password = post('password');
        $length = strlen($password);
        if($length<6 || $length>20){
            return  $this->asErrorJson('密码长度为6-20个字符!');
        }

        /** @var $model User */
        $model = user()->identity;
        $model->password_hash = $password;
        if($model->save()){
            return  $this->asSuccessJson();
        }
        return  $this->asErrorJson('修改密码失败!!');
    }

    /**
     * actionTheme
     * @return \yii\web\Response
     */
    public function actionTheme()
    {
        $theme = post('theme','skin-blue');
        session()->set('theme',$theme);
        return  $this->asSuccessJson();
    }
}
